package com.example.demo.action;

import com.example.demo.dao.ToolAdminMapper;
import com.example.demo.dao.ToolUserMapper;
import com.example.demo.pojo.Tool;
import com.example.demo.pojo.tool_Admin;
import com.example.demo.pojo.tool_User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service
@Controller
@RequestMapping("/tooladmin")
public class ToolAdminAction {

    @Autowired
    ToolAdminMapper toolAdminMapper;



    @RequestMapping("/logins")
    public String logins(String admin_usernameCode,String admin_passwordCode, HttpSession session) {
        System.out.println(admin_usernameCode+ "----" + admin_passwordCode);

        tool_Admin u=toolAdminMapper.findToolAdmin(admin_usernameCode,admin_passwordCode);
        if(u!=null){
            session.setAttribute("admininfo",u);
            return "welcome";
        }

        return "adminlogin";
    }

    @RequestMapping(value = "/tools")
    public String findAll(Model model,
                          @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<Tool> tools = toolAdminMapper.selectAlltool();
        PageInfo<Tool> pageInfo = new PageInfo<>(tools);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("tools",tools );
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("我睡了3秒……");
        return "welcome";
    }


    @RequestMapping(value = "/select")
    public String findByCode(Model model, String tool_idCode,
                             @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<Tool> tools = toolAdminMapper.selectByid(tool_idCode);
        PageInfo<Tool> pageInfo = new PageInfo<>(tools);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("tools", tools);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
        return "welcome";
    }

    @RequestMapping(value = "edit/{tool_id}")
    @ResponseBody
    public Tool findToolById(@PathVariable String tool_id) {
        System.out.println("这里是获取到的id"+tool_id);
        Tool tool = toolAdminMapper.selectByKey(tool_id);
        return tool;
    }

    @PutMapping("/edit")
    public String editTool(Tool tool) {
        toolAdminMapper.editTool(tool);
        return "welcome";
    }

    @RequestMapping(value = "del_one/{tool_id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delUserById(@PathVariable String tool_id) {
        System.out.println(tool_id);
        toolAdminMapper.deleteByid(tool_id);
        return "welcome";
    }

    @PostMapping("/add")
    public String addUser(@Validated Tool tool) {
        System.out.println(tool.toString());
        toolAdminMapper.addTool(tool);
        return "welcome";
    }




    @RequestMapping(value = "/users")
    public String findAlluser(Model model,
                          @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<tool_User> users = toolAdminMapper.selectAlluser();
        PageInfo<tool_User> pageInfo = new PageInfo<>(users);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("users",users );
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
//        try {
//            Thread.sleep(3000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("我睡了3秒……");
        return "welcomeusercontrol";
    }


    @PostMapping("/adduser")
    public String addUser(@Validated tool_User user) {
        System.out.println(user.toString());
        toolAdminMapper.addUser(user);
        return "welcomeusercontrol";
    }


    @RequestMapping(value = "/selectuser")
    public String findByuserCode(Model model, String user_idCode,
                             @RequestParam(value = "pageNow", required = false, defaultValue = "1") Integer pageNow,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<tool_User> users = toolAdminMapper.selectByuserid(user_idCode);
        PageInfo<tool_User> pageInfo = new PageInfo<>(users);
        if (pageNow == 1) {
            pageInfo.setPrePage(1);
        }
        if (pageInfo.getNextPage() == 0) {
            pageInfo.setNextPage(pageInfo.getPrePage() + 1);
        }
        model.addAttribute("users", users);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("pageNow", pageNow);
        return "welcomeusercontrol";
    }



    @RequestMapping(value = "del_oneuser/{user_id}", method = RequestMethod.DELETE)
    @ResponseBody
    public String delUserByuserId(@PathVariable String user_id) {
        System.out.println(user_id);
        toolAdminMapper.deleteByuserid(user_id);
        return "welcomeusercontrol";
    }



    @RequestMapping(value = "edituser/{user_id}")
    @ResponseBody
    public tool_User findUserByuserId(@PathVariable String user_id) {
        System.out.println("这里是获取到的id"+user_id);
        tool_User user = toolAdminMapper.selectByuserKey(user_id);
        return user;
    }

    @PutMapping("/edituser")
    public String editUser(tool_User user) {
        toolAdminMapper.editUser(user);
        return "welcomeusercontrol";
    }

}
